﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Printing;
using System.Drawing;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;

namespace QLBV
{
    public partial class KeDon : System.Web.UI.Page
    {
        SqlConnection conn;
        String strCon = "Data Source=HTD-COM;Initial Catalog=QLBV;Integrated Security=True;Pooling=False";

        protected void Page_Load(object sender, EventArgs e)
        {
            Load_Table();
        }
        public void Load_Table()
        {
            lbMsg.Text = "";
            String strSelect = "SELECT * FROM Thuoc ";
            conn = new SqlConnection(strCon);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(strSelect, conn);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            gvThuoc.DataSource = dt;
            gvThuoc.DataBind();
            conn.Close();
         }
        public void Load_Table2()
        {
             lbMsg.Text = "";
            String strSelect = "SELECT * FROM DonThuoc where MaBN='"+txtMaBN.Text+"'";
            conn = new SqlConnection(strCon);
              conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(strSelect, conn);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            gvDonThuoc.DataSource = dt;
            gvDonThuoc.DataBind();
            conn.Close();
        }
        protected void btSearch_Click(object sender, EventArgs e)
        {
            if (txtTk.Text != " ")
            {
                conn.Open();
                SqlCommand cmdBN = new SqlCommand();
                cmdBN.Connection = conn;
                cmdBN.CommandText = "Select * from BenhNhan Where MaBN= '" + txtTk.Text + "'";
                SqlDataReader reader = cmdBN.ExecuteReader();
                if (reader.Read())
                {
                    txtMaBN.Text = reader.GetValue(0).ToString();
                    txtCanBenh.Text = reader.GetValue(5).ToString();
                    txtName.Text = reader.GetValue(1).ToString();
                    txtAddress.Text = reader.GetValue(2).ToString();
                    txtGender.Text = reader.GetValue(7).ToString();
                    txtPhone.Text = reader.GetValue(3).ToString();
                    Load_Table2();
                }
                else lbMsg.Text = "Không có bệnh nhân mà bạn tìm kiếm!";
                txtTk.Text = "";
                reader.Close();
                conn.Close();
              
            }
            else lbMsg.Text = "Vui lòng nhập mã bệnh nhân!";
            
        }

        protected void gvThuoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvThuoc.PageIndex = e.NewPageIndex;
            gvThuoc.DataBind();
        }

        protected void gvThuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
             String strThuoc = "Select * from Thuoc where MaThuoc='" + gvThuoc.SelectedRow.Cells[1].Text + "'";
           conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strThuoc, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Thuoc");
            txtMT.Text = ds.Tables["Thuoc"].Rows[0][0].ToString();
            txtTT.Text=ds.Tables["Thuoc"].Rows[0][1].ToString();
           lbDV1.Text=lbDV2.Text = ds.Tables["Thuoc"].Rows[0][3].ToString();
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String strInsertThuoc = "INSERT INTO DonThuoc (MaBN,MaThuoc,TenThuoc,SoLuong,NgayUong,LanUong,DonVi)VALUES (@mabn,@mathuoc,@tenthuoc,@soluong,@ngayuong,@lanuong,@donvi)";
         
            if (txtMaBN.Text==""||
                txtMT.Text == "" ||
          txtNU.Text == "" ||
          txtLU.Text == "" ||
          txtSL.Text == "")
            {
                lbMsg.Text = "Vui lòng nhập đầy đủ thông tin ";
            }
            else
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(strInsertThuoc, conn);
                cmd.Parameters.Add("@mabn", txtMaBN.Text);
                cmd.Parameters.Add("@mathuoc", txtMT.Text);
                cmd.Parameters.Add("@tenthuoc", txtTT.Text);
                cmd.Parameters.Add("@soluong", txtSL.Text);
                cmd.Parameters.Add("@ngayuong", txtNU.Text);
                cmd.Parameters.Add("@lanuong", txtLU.Text);
                cmd.Parameters.Add("@donvi", lbDV1.Text);
                cmd.ExecuteNonQuery();
                reset();
                conn.Close();
                Load_Table2();
            }
        }
        public void reset()
        { 
          txtMT.Text = "";;
          txtNU.Text = "";
          txtLU.Text = "";
          txtSL.Text = "";
          txtTT.Text = "";
          lbDV1.Text = lbDV2.Text = "";
        }

     
        protected void btUpdate_Click(object sender, EventArgs e)
        {
            String strUpdate = "UPDATE DonThuoc SET  SoLuong=@soluong, NgayUong=@ngayuong,LanUong=@lanuong WHERE MaThuoc = '" + txtMT.Text + "' and MaBN='"+txtMaBN.Text+"'";
            if (txtMaBN.Text == "" || 
                txtMT.Text == "" ||
          txtNU.Text == "" ||
          txtLU.Text == "" ||
          txtSL.Text == "")
            {
                lbMsg.Text = "Vui lòng nhập đầy đủ thông tin ";
            }
            else
            {
                conn.Open();
                //update don thuoc
                SqlCommand cmd = new SqlCommand(strUpdate, conn);
                cmd.Parameters.Add("@soluong", txtSL.Text);
                cmd.Parameters.Add("@ngayuong", txtNU.Text);
                cmd.Parameters.Add("@lanuong", txtLU.Text);
               cmd.ExecuteNonQuery();
                conn.Close();
                reset();
                Load_Table2();
            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            String strDelete = "DELETE FROM DonThuoc WHERE MaThuoc = '" + txtMT.Text + "'";
            if (txtMT.Text != "" && txtMaBN.Text != "")
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(strDelete, conn);
                cmd.ExecuteNonQuery();
                reset();
                conn.Close();
                reset();
                Load_Table2();
            }
            else lbMsg.Text = "Vui lòng chọn thuốc để xóa!";
            
        }

        protected void gvDonThuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            String strThuoc = "Select * from DonThuoc where MaThuoc='" + gvDonThuoc.SelectedRow.Cells[2].Text + "'";
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strThuoc, conn);         
            DataSet ds = new DataSet();
           //fil Don Thuoc
            da.Fill(ds, "DonThuoc");
            txtMaBN.Text = ds.Tables["DonThuoc"].Rows[0][0].ToString();
            txtMT.Text = ds.Tables["DonThuoc"].Rows[0][1].ToString();
            txtSL.Text = ds.Tables["DonThuoc"].Rows[0][2].ToString();
            txtNU.Text = ds.Tables["DonThuoc"].Rows[0][3].ToString();
            txtLU.Text = ds.Tables["DonThuoc"].Rows[0][4].ToString();
          lbDV2.Text = lbDV1.Text = ds.Tables["DonThuoc"].Rows[0][5].ToString();
            //fill Benh nhan
          String strBN = "Select * from BenhNhan where MaBN='" + txtMaBN.Text + "'";
          SqlDataAdapter daBN = new SqlDataAdapter(strBN, conn);
          daBN.Fill(ds, "BenhNhan");
          txtCanBenh.Text = ds.Tables["BenhNhan"].Rows[0][5].ToString();
          txtName.Text = ds.Tables["BenhNhan"].Rows[0][1].ToString();
          txtAddress.Text = ds.Tables["BenhNhan"].Rows[0][2].ToString();
          txtGender.Text = ds.Tables["BenhNhan"].Rows[0][7].ToString();
          txtPhone.Text = ds.Tables["BenhNhan"].Rows[0][3].ToString();
          conn.Close();
        }

        protected void gvDonThuoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDonThuoc.PageIndex = e.NewPageIndex;
            gvDonThuoc.DataBind();
        }
        protected void Print_Click(object sender, EventArgs e)
        {
            if (txtMaBN.Text == "")
            {
                lbMsg.Text = "Không có bệnh nhân được chọn";
            }
            else
            {
                Session["name"] = txtName.Text;
                Session["gender"] = txtGender.Text;
                Session["canbenh"] = txtCanBenh.Text;
                Session["maBN"] = txtMaBN.Text;

                Response.Redirect("testExport.aspx");
            }
        }       
    }
}